package cn.zzdt4j.config.verify;

import cn.zzdt4j.config.service.biz.ConfigModificationVerifyService;
import cn.zzdt4j.core.config.ApplicationContextHolder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * The type Config modification verify service choose.
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023 /11/12 21:18
 */
@Component
// public class ConfigModificationVerifyServiceChoose implements InitializingBean {
public class ConfigModificationVerifyServiceChoose implements CommandLineRunner {// 这里使用CommandLineRunner， 会出现ApplicationContextHolder.setApplicationContext还没有被调用

    /**
     * Storage config change verify service container
     */
    private final Map<Integer, ConfigModificationVerifyService> configChangeVerifyServiceChooseMap = new HashMap<>();


    /**
     * Choose config modification verify service.
     *
     * @param type the type
     * @return the config modification verify service
     */
    public ConfigModificationVerifyService choose(Integer type) {
        return configChangeVerifyServiceChooseMap.get(type);
    }

    @Override
    public void run(String... args) throws Exception {
        final Map<String, ConfigModificationVerifyService> modificationVerifyServiceMap =
                ApplicationContextHolder.getBeanOfType(ConfigModificationVerifyService.class);
        modificationVerifyServiceMap.values().forEach(each -> configChangeVerifyServiceChooseMap.put(each.type(), each));
    }
}
